package com.plian.system.controller.qg.subsidiary;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.dto.qg.subsidiary.SubsidiaryIntroduceReformDTO;
import com.plian.system.entity.qg.subsidiary.SubsidiaryIntroduceReform;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.qg.subsidiary.ISubsidiaryIntroduceReformService;
import com.plian.system.service.wf.FormWorkflowService;
import com.plian.system.vo.qg.subsidiary.SubsidiaryIntroduceReformVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.wrapper.qg.subsidiary.SubsidiaryIntroduceReformWrapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

/**
 * 引进非市级国有资本 控制器
 *
 * @author jianglei
 * @since 2019-11-05
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/subsidiaryintroducereform")
public class SubsidiaryIntroduceReformController extends BaseController {

    private ISubsidiaryIntroduceReformService subsidiaryIntroduceReformService;

    private FormWorkflowService formWorkflowService;

    private SubsidiaryIntroduceReformWrapper subsidiaryIntroduceReformWrapper;

    /**
     * 详情
     */
    @PostMapping("/detail")
    public R<SubsidiaryIntroduceReformVO> detail(@Valid @RequestBody SubsidiaryIntroduceReform subsidiaryIntroduceReform) {
        SubsidiaryIntroduceReform detail = subsidiaryIntroduceReformService.getOne(Condition.getQueryWrapper(subsidiaryIntroduceReform));
        SubsidiaryIntroduceReformVO subsidiaryIntroduceReformVO = subsidiaryIntroduceReformWrapper.entityToVO(detail);
        return subsidiaryIntroduceReformService.getDetailObj(subsidiaryIntroduceReformVO);
    }

    /**
     * 分页 引进非市级国有资本
     */
    @PostMapping("/list")
    public ListResult<SubsidiaryIntroduceReformVO> list(@Valid @RequestBody Query query) {
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("create_time");
        }

        List<SearchEntity> entityList = query.getSearch();
        QueryWrapper<SubsidiaryIntroduceReform> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new SubsidiaryIntroduceReform()), entityList
        );
        TokenSearch<SubsidiaryIntroduceReform> tokenSearch = new TokenSearch<>();
        tokenSearch.addTokenQuery(queryWrapper, query);

        IPage<SubsidiaryIntroduceReform> pages = subsidiaryIntroduceReformService.page(Condition.getPage(query), queryWrapper);
        ListResult<SubsidiaryIntroduceReformVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(subsidiaryIntroduceReformWrapper.entityToVO((pages).getRecords()));
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());
        return listResult;
    }


    /**
     * 新增或修改 引进非市级国有资本
     */
    @PostMapping("/submit")
    @LogOperation("引进非市级国有资本-提交")
    @Resubmit(delaySeconds = 0)
    public R submit(@Valid @RequestBody SubsidiaryIntroduceReform subsidiaryIntroduceReform) {
        boolean result = subsidiaryIntroduceReformService.submit(subsidiaryIntroduceReform);
        HashMap<String, Object> processVariable = new HashMap<>();
        formWorkflowService.startProcessAndSimulate(String.valueOf(subsidiaryIntroduceReform.getId()), subsidiaryIntroduceReform.getFormType(), processVariable);
        return result ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }


    /**
     * 删除 引进非市级国有资本
     */
    @PostMapping("/remove")
    @LogOperation("引进非市级国有资本-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return subsidiaryIntroduceReformService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    /**
     * 引进非市级国有资本 统计表
     */
    @PostMapping("/introduceStatistics")
    public R<ReportReturnVO> introduceStatistics(@Valid @RequestBody(required = false) SubsidiaryIntroduceReformDTO subsidiaryIntroduceReformDTO) {
        ReportReturnVO reportReturnVO = subsidiaryIntroduceReformService.introduceStatistics(subsidiaryIntroduceReformDTO);
        R<ReportReturnVO> results = new R(CommonCode.SUCCESS);
        results.setPageData(reportReturnVO);
        return results;
    }

    /**
     * 引进非市级国有资本信息情况报表
     *
     * @param subsidiaryIntroduceReformDTO
     * @return
     */
    @PostMapping("/introduceCapitalInfoReport")
    public R<ReportReturnVO> introduceCapitalInfoReport(@RequestBody SubsidiaryIntroduceReformDTO subsidiaryIntroduceReformDTO) {
        ReportReturnVO reportReturnVO = subsidiaryIntroduceReformService.introduceCapitalInfoReport(subsidiaryIntroduceReformDTO);
        R<ReportReturnVO> results = new R(CommonCode.SUCCESS);
        results.setPageData(reportReturnVO);
        return results;
    }

    /**
     * 引进非市级国有资本信息情况导出
     *
     * @param subsidiaryIntroduceReform
     * @param response
     */
    @PostMapping("/exportDetailExcel")
    public void exportDetailExcel(@Valid @RequestBody SubsidiaryIntroduceReform subsidiaryIntroduceReform, HttpServletResponse response) {
        SubsidiaryIntroduceReform detail = subsidiaryIntroduceReformService.getOne(Condition.getQueryWrapper(subsidiaryIntroduceReform));
        SubsidiaryIntroduceReformVO subsidiaryIntroduceReformVO = subsidiaryIntroduceReformWrapper.entityToVO(detail);
        subsidiaryIntroduceReformService.exportDetailExcel(subsidiaryIntroduceReformVO, response);
    }

}
